clear all
set more off, permanently
set type double
cd ""

* Log file
capture log close
global date "`c(current_date)'"
log using "log_files/figure_$date.txt", text replace

****************
*** FIGURE 1 ***
****************

capture program drop lpolygraph 							
program define lpolygraph
	args y x yt xt 

	preserve

	capture drop dembin
	g dembin = int(`x'*80)/80 
	egen Ey = mean(`y'), by(dembin)

	collapse `x' `y' if dembin~=., by(dembin)

	lpoly `y' `x' if `x'<=.5 // default is kernel(epanechnikov)
	lpoly `y' `x' if `x'>.5 


	lpoly `y' `x' if `x'<=0.5,  gen(z0 s0) ci se(se0)
	lpoly `y' `x' if `x'>0.5,  gen(z1 s1) ci se(se1)
	forvalues v=0/1 {
		gen ul`v' = s`v' + 1.96*se`v' 
		gen ll`v' = s`v' - 1.96*se`v' 
	}

	tw (scatter `y' `x', msymbol(oh) sort) /*
		*/ (line ul0 ll0 s0 z0, lcolor(black black black) lpattern(dash dash solid))  /*
		*/ (line ul1 ll1 s1 z1, lcolor(black black black) lpattern(dash dash solid)),  legend(off)  /*
		*/ xline(0.5, lpattern(solid) lwidth(thin) lcolor(gs5)) /*
		*/ xtitle(`xt') ytitle(`yt')  ylabel(, nogrid) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin))
		
	drop   ul0 ll0 s0 z0 ul1 ll1 s1 z1 dembin
	restore
end	

use "base_main.dta", clear 

	lpolygraph yrs_edu voterecall_lag "Years of Education (t)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsEdu_lpoly_80_pub", replace
	graph export "figures/YrsEdu_lpoly_80_pub.eps", as(eps) preview(on) replace
	lpolygraph Uni voterecall_lag " Attended University (t) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Uni_lpoly_80_pub", replace
	graph export "figures/Uni_lpoly_80_pub.eps", as(eps) preview(on) replace
	lpolygraph Tec voterecall_lag "Attended Technical Education (t) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Technical_lpoly_80_pub", replace
	graph export "figures/Technical_lpoly_80_pub.eps", as(eps) preview(on) replace
	lpolygraph Sec2 voterecall_lag "Completed Secondary (t) (%)" "Recall Vote Share (t-1)"  
	graph export "figures/Secondary_lpoly_80_pub.eps", as(eps) preview(on) replace
	graph save "figures/Secondary_lpoly_80_pub", replace
	graph combine "figures/YrsEdu_lpoly_80_pub" "figures/Uni_lpoly_80_pub" "figures/Technical_lpoly_80_pub" "figures/Secondary_lpoly_80_pub", graphregion(color(white))
	graph export "figures/Edu_Cand_lpoly_80_pub.eps", as(eps) preview(on) replace

log close
